<?php
namespace app\index\controller;
use think\Db;
class Publics extends Base
{
    //短信提示
    public function sendSms(){
        if(is_post()){
            $type  = input('type');
            $s       = new \app\common\model\Sms();
            $code    = rand(1000,9999);
            switch ($type) {
                case 'task':
                    $phone = input('phone');
                    if( !preg_match('/^1\d{10}$/', $phone) ) com_error('手机号码不正确');
                    $content = "您的验证码是{$code}。如非本人操作，请忽略本短信。";
                    $tpl_val = json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE);
                    $no = 1843144;
            }
            $id = $s->create($phone, $content, $type, $code, $no, $tpl_val);
            $s->sendOne($id);
            com_check();
        }
    }
    /**
     * 退出
     */
    public function logOut()
    {
        cookie('phone',null);
        r(url('Publics/logIn'));
    }
    /**
     * 登陆
     */
    public function logIn()
    {
        if (is_post()) {
            $phone = input('phone');
            $pwd = input('pwd');
            // 登录
            $rs = Db::name("user")->where("phone",$phone)->where("pwd",com_password($pwd))->find();
            if(!$rs){
                com_error('登录失败!');
            }else{
                cookie('phone',$phone,60*60*24*30);
            }
            com_check(['suc_msg'=>'登录成功!','suc_url'=>url('index/index')]);
        }
        return view();
    }
}
